package com.dalin.framework.dao;

import com.dalin.framework.dao.imple.MysqlConnection;
import com.dalin.framework.dao.imple.OracleConnection;
import com.dalin.framework.dao.imple.SqlserverConnection;
import com.dalin.framework.vos.JdbcVo;
import com.dalin.common.PropertyConst;

/*****************************************************************************
 * @content: 数据库连接工厂                                                   *
 * @author : 那建林                                                          *
 * @crtDate: 2009-06-15                                                     *
 ****************************************************************************/
public class ConnectionFactory {
	private ConnectionFactory() { // 不允许new此对象
	}
	
	/*************************************************************************
	 * @content: 根据JdbvVo值对象来动态创建相应的DB连接
	 * @param  : JdbcVo
	 * @return : DbConn接口
	 * @author : 那建林
	 * @crtDate: 2009-06-15
	 */
	public static DbConn getConnectionInstance(JdbcVo vo) throws Exception {
		String driverClassName = vo.getDriverClassName(); // vo driver串
		String name = vo.getUserName(); // 登陆用户名
		String pswd = vo.getUserPswd(); // 登陆密码
		String url = null; // 连接路径
		if (PropertyConst.DRIVER_MSSQL_2000.equals(driverClassName)) {
			url = "jdbc:microsoft:sqlserver://" + vo.getHostIp()
				  + PropertyConst.COLON + vo.getPort()
				  + ";SelectMethod=cursor;DatabaseName=" + vo.getDbName();
			return new SqlserverConnection(driverClassName, url, name, pswd);
		} else if (PropertyConst.DRIVER_MSSQL_2005.equals(driverClassName)){
			url = "jdbc:sqlserver://" + vo.getHostIp()
				  + PropertyConst.COLON + vo.getPort()
				  + ";databaseName=" + vo.getDbName();
			return new SqlserverConnection(driverClassName, url, name, pswd);
		} else if (PropertyConst.DRIVER_ORACLE.equals(driverClassName)){
			url = "jdbc:oracle:thin:@" + vo.getHostIp()  
				  + PropertyConst.COLON + vo.getPort()
				  + PropertyConst.COLON + vo.getDbName();
			return new OracleConnection(driverClassName, url, name, pswd);
		} else if (PropertyConst.DRIVER_MYSQL.equals(driverClassName)){
			url = "jdbc:mysql://" + vo.getHostIp()
				+":" + vo.getPort() + "/"+vo.getDbName()
				+ "?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
			return new MysqlConnection(driverClassName, url, name, pswd);
		}
		throw new RuntimeException("无法认知的驱动字符串");
	}
}
